<?php
namespace app\models;

use pqphp\base\Model;
use pqphp\db\Db;

/**
 * 用户Model
 */
class PayNoticeModel extends Model
{
    /**
     * 客户唯一标识
     * 支付前的人微信唯一标示
     * 商户号
     * 微信公众号ID
     * 订单支付金额
	 * 实际支付金额
	 * 状态
     */
	public function recordwechat($openid,$mid,$appid,$total_fee,$cash_fee,$state,$transaction_id,$out_trade_no,$trade_type,$attach,$result_code,$sign)
	{
		$data['createtime']=date('Y-m-d H:i:s', time());
		$data['openid']=$openid;
		$data['mid']=$mid;
		$data['appid']=$appid;
		$data['total_fee']=$total_fee;
		$data['cash_fee']=$cash_fee;
		$data['state']=$state;
		$data['transaction_id']=$transaction_id;
		$data['out_trade_no']=$out_trade_no;
		$data['trade_type']=$trade_type;
		$data['attach']=$attach;
		$data['result_code']=$result_code;
		$data['sign']=$sign;
		$this->insertData($data,'payresult');
	}

	public function recordunion($data)
	{
		$data['createtime']=date('Y-m-d H:i:s', time());
		$this->insertData($data,'payresult');
	}

	/**
     * @商户微信公众号appid
     * @商户号
     */
	public function findwechatkey($appid,$mid)
	{
		$sql = "select `key` from wechatpay where appid='".$appid."' and mch_id='$mid'";
		//exit($sql);
        $sth = Db::pdo()->prepare($sql);
        $sth = $this->formatParam($sth);
        $sth->execute();
        $result=$sth->fetchAll();
		if(count($result) == 0){
			$this->paylogs("查找微信支付用户证书秘钥错误");
		}
		return $result[0]['key'];
	}


	/**
     * @微信支付是否已经通知成功
     * @商户号
     */
	public function findwechatresult($mch_id,$appid,$out_trade_no)
	{
		$sql = "select count(*) as len from payresult where mid='$mch_id' and appid='$appid' and out_trade_no='$out_trade_no' and result_code='SUCCESS'";
        $sth = Db::pdo()->prepare($sql);
        $sth = $this->formatParam($sth);
        $sth->execute();
        $result=$sth->fetchAll();
		return $result[0]['len'];
	}


	
	/**
     * @银联支付是否已经通知成功
     * @商户号
     */
	public function findunionresult($mid,$tid,$merOrderId)
	{
		$sql = "select count(*) as len from payresult where mid='$appid' and tid='$tid' and merOrderId='$merOrderId' and status='TRADE_SUCCESS'";
        $sth = Db::pdo()->prepare($sql);
        $sth = $this->formatParam($sth);
        $sth->execute();
        $result=$sth->fetchAll();
		return $result[0]['len'];
	}


	/**
     * @银联终端号tid
     * @商户号
     */
	public function findunionkey($tid,$mid)
	{
		$sql = "select `key` from unionpay where tid='$tid' and mid='$mid'";
		//exit($sql);
        $sth = Db::pdo()->prepare($sql);
        $sth = $this->formatParam($sth);
        $sth->execute();
        $result=$sth->fetchAll();
		if(count($result) == 0){
			$this->paylogs("查找银联支付用户证书秘钥错误");
		}
		return $result[0]['key'];
	}
}